package com.example.springmac;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.rmi.StubNotFoundException;
import java.util.Arrays;
import java.util.List;

class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/hello")
    public String say() {
        return "hello, Spring mvc1";
    }

    @RequestMapping("/r1")
    public String r1(String name) {
        return "接收到参数, name= " + name;
    }

    @RequestMapping("/r2")
    public String r2(Integer age) {
        return "接收到参数, age= " + age;
    }

    @RequestMapping("/r3")
    public String r3(int age) {
        return "接收到参数, age= " + age;
    }

    @RequestMapping("/r4")
    public String r4(String name, Integer age) {
        return "接收到参数, name= " + name + ", age = " + age;
    }

    @RequestMapping("/r5")
    public String r5(Student student) {
        return "接收到参数: " + student.toString();
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String username, Integer age) {
        return "接收到参数, name= " + username + ", age = " + age;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name", required = false) String name) {
        return "接收到参数, name= " + name;
    }

    @RequestMapping("/r8")
    public String r8(int[] array) {
        return "接收到参数" + Arrays.toString(array);
    }

    @RequestMapping("/r9")
    public String r9(@RequestParam List<String> list) {
        return "接收到参数" + list;
    }
    @RequestMapping("/r10")
    public String r10(@RequestBody Student student) {
        return "接收到参数: " + student.toString();
    }

    @RequestMapping("/article/{articleId}")
    public String r11(@PathVariable("articleId") Integer articleId) {
        return "接收到参数, articleId:" + articleId;
    }

    @RequestMapping("/article/{articleId}/{name}")
    public String r12(@PathVariable Integer articleId, @PathVariable("name") String articleName) {
        return "接收到参数, articleId:" + articleId + " , name: " + articleName;
    }

    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        return "接收到文件, 文件名: " + originalFilename;
    }
}
